package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.image.Images;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.http.ThingsHttpModel;
import com.monkeygroup.mygoplus.thing.vo.MyImageVO;
import com.monkeygroup.mygoplus.thing.vo.RentVO;
import com.monkeygroup.mygoplus.thing.vo.UserVO;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;
import com.monkeygroup.mygoplus.user.vo.UserCollectVO;
import com.monkeygroup.mygoplus.view.home.activity.PublishedMineActivity;
import com.monkeygroup.mygoplus.view.home.activity.ThingsDetailActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的收藏
 * Created by cumt on 2017/6/28.
 */

public class PersonCollectActivity extends BaseActivity implements View.OnClickListener{

    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PersonCollectActivity.class);

        activity.startActivity(intent);
    }


    private Activity activity;

    private LinearLayout ll_rent,ll_sell,ll_top,ll_buiding_case;
    private TextView txt_rent,txt_sell,txt_top,txt_buiding_case;

    private LinearLayout ll_data,default_page;

    private PullToRefreshListView lv_result;

    private List<RentVO> sellList;
    private SellLeftAdapter adapter;

    //头部
    private TextView txtTitle;
    private ImageView imgBack;


    //
//    private ThingsHttpModel httpThingsModel;
//    private Map<String,String> param;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_collect);
    }

    @Override
    public void initView() {

        activity = PersonCollectActivity.this;
        txtTitle =  (TextView)findViewById(R.id.txt_title);
        txtTitle.setText("我的物件收藏");
        imgBack =  (ImageView) findViewById(R.id.img_back);

        ll_rent = (LinearLayout)findViewById(R.id.ll_rent);
        ll_sell= (LinearLayout)findViewById(R.id.ll_sell);
        ll_top= (LinearLayout)findViewById(R.id.ll_top);
        ll_buiding_case= (LinearLayout)findViewById(R.id.ll_buiding_case);

        txt_rent= (TextView)findViewById(R.id.txt_rent);
        txt_sell= (TextView)findViewById(R.id.txt_sell);
        txt_top= (TextView)findViewById(R.id.txt_top);
        txt_buiding_case= (TextView)findViewById(R.id.txt_top);


        ll_data= (LinearLayout)findViewById(R.id.ll_data);
        default_page= (LinearLayout)findViewById(R.id.default_page);

        lv_result = (PullToRefreshListView) findViewById(R.id.lv_result);
        sellList = new ArrayList<>();
        adapter =  new SellLeftAdapter(PersonCollectActivity.this);
        lv_result.setAdapter(adapter);

//        param = new HashMap<>();


        PersonalNomalInfo user = LocalInfo.getUserInfo(activity);
        if(user!=null){


            UserCollectVO colect = LocalInfo.getUserCollectInfo(activity,user.getMobile());
            if(colect!=null){

                ll_data.setVisibility(View.VISIBLE);
                default_page.setVisibility(View.GONE);

                String[] collect = colect.getCollectSeq().split(",");
                for(int i=0;i<collect.length;i++){
                     Map<String,String> param = new HashMap<>();

                    Log.e("collect[i]==",collect[i]);
                    param.remove("seq");
                    param.put("seq",collect[i]);
                    ThingsHttpModel httpThingsModel = initRequest(param);
                    httpThingsModel.requestThings();

                }


            }else{

                ll_data.setVisibility(View.GONE);
                default_page.setVisibility(View.VISIBLE);
            }

        }
        else{

            ll_data.setVisibility(View.GONE);
            default_page.setVisibility(View.VISIBLE);
        }



    }

    @Override
    public void setListener() {

    }

    @Override
    public void doEvent() {


        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();

            }
        });
    }

    public class SellLeftAdapter extends BaseAdapter {

        private LayoutInflater inflater;

        public SellLeftAdapter(Context context){
            this.inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return sellList.size();
        }

        @Override
        public Object getItem(int position) {
            return sellList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            convertView = inflater.inflate(R.layout.person_collect_item, null);
            final RentVO obj = sellList.get(position);
            final SimpleDraweeView txtName = (SimpleDraweeView) convertView
                    .findViewById(R.id.img_sell_house_item);
            final TextView txt_sell_house_item_name = (TextView) convertView
                    .findViewById(R.id.txt_sell_house_item_name);
            final TextView txt_sell_house_item_price = (TextView) convertView
                    .findViewById(R.id.txt_sell_house_item_price);
            final TextView txt_sell_house_item_address = (TextView) convertView
                    .findViewById(R.id.txt_sell_house_item_address);


            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            if(obj!=null){
                Images.with(PersonCollectActivity.this).load(Uri.parse(obj.getImg())).resize(130,90).into(txtName);
                txt_sell_house_item_name.setText(obj.getName());
                txt_sell_house_item_price.setText(String.format("坪数：%s",obj.getLawnarea())+"坪");
                txt_sell_house_item_address.setText(String.format("售价：%s",""+obj.getSaleprice()));
            }

            return convertView;
        }



    }

    private ThingsHttpModel initRequest(Map param){

        ThingsHttpModel  httpThingsModel = new ThingsHttpModel(RequestUrl.getPropertyUrl , param,
                new ThingsHttpModel.SimpleOnHttpRequestListener() {
                    @Override
                    public void onRequestFinished(String resultCode, Integer timestamp) {

                    }

                    @Override
                    public void onRequestFinished(String resultCode, Object property, Object user, Object users, Object circuit, Object img) {
                        Gson gson = new Gson();
                        List<MyImageVO> imgModel = gson.fromJson(gson.toJson(img), new TypeToken<List<MyImageVO>>() {
                        }.getType());
                        RentVO propertyModel = gson.fromJson(gson.toJson(property), new TypeToken<RentVO>() {
                        }.getType());
                        UserVO userVO = gson.fromJson(gson.toJson(user), new TypeToken<UserVO>() {
                        }.getType());

                        sellList.add(propertyModel);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onBuildingRequestFinished(String resultCode, Object building, Object user, Object users, Object circuit, Object img) {

                    }

                    @Override
                    public void onCommunityRequestFinished(String resultCode, Object community, Object user, Object users, Object circuit, Object img) {

                    }
                }, true);

        return httpThingsModel;

    }


}
